/**
 * 
 */
package com.xb.cloud.product.merchant.inner.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.JsonUtils;
import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.product.merchant.inner.service.ProductSkuInnerService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ProductSkuInputBo;
import com.xb.cloud.product.merchant.service.impl.AbstractService;
import com.xb.cloud.product.merchant.service.impl.ProductPublishServiceImpl;
import com.xb.cloud.product.service.business.model.ProductSku;
import com.xb.cloud.product.service.business.model.mapper.ProductSkuMapper;

/**
 * @author kexue
 *
 */
@Service
public class ProductSkuInnerServiceImpl extends AbstractService  implements ProductSkuInnerService{

	@Resource
	private ProductSkuMapper skuMapper;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ProductPublishServiceImpl.class);

	/**
	 * 保存sku 
	 */
	@Override
	public Long createSku(ProductSkuInputBo skuInput,EndMerchantUserBo endMerchantUserBo) {
		
		Long generateSkuId = ipKeyGenerator.generateKey();
		
		ProductSku sku =new ProductSku();
		sku.setId(generateSkuId); 
		sku.setCompanyId(endMerchantUserBo.getCompanyId());
		sku.setMerchantId(endMerchantUserBo.getMerchantId());
		sku.setShopId(endMerchantUserBo.getShopId());
		
		sku.setProductId(skuInput.getProductId()); 
		//上架 或者仓库
		sku.setIsCanSale(skuInput.getIsCanSale());
		sku.setPaymentType(skuInput.getPaymentType());
		sku.setDeliveryDesc(skuInput.getDeliveryDesc());
		sku.setSkuNameCn(skuInput.getSkuNameCn());
		sku.setSkuTitleSub(skuInput.getSkuTitleSub());
		sku.setSkuUnit(skuInput.getSkuUnit());
		
		sku.setCreateTime(timeService.getDate());
		sku.setCreateUserId(endMerchantUserBo.getId());
		sku.setCreateUserName(endMerchantUserBo.getLoginName());
		
		//TODO 更新时间&创建时间同步 ，创建的时候
		sku.setUpdateTime(timeService.getDate());
		sku.setUpdateUserId(endMerchantUserBo.getId());
		sku.setUpdateUserName(endMerchantUserBo.getLoginName());
		
		int rows = skuMapper.insertSelective(sku);
		if(rows>0){
			return generateSkuId;
		}else{
			String message = "保存sku失败"+JsonUtils.beanToJson(sku);
			LOGGER.error(message);
			throw new RuntimeException(message); 
		}
	} 
	
	/**
	 * 更新sku 
	 */
	@Override
	public Long updateSku(ProductSkuInputBo skuInput,EndMerchantUserBo endMerchantUserBo) {
		
		Long skuId = skuInput.getId();
		
		//验证是否存在、被删除
		ProductSku sku =skuMapper.selectByPrimaryKey(skuId);
		if(sku==null||sku.getIsDeleted().equals(DbConstants.IS_DELETED_TRUE)){
			String message = "未找到所更新sku，或已被删除"+JsonUtils.beanToJson(sku);
			LOGGER.error(message);
			throw new RuntimeException(message); 
		}
		
		ProductSku newSku = new ProductSku();
		newSku.setId(skuInput.getId()); 
		//上架 或者仓库
		newSku.setIsCanSale(skuInput.getIsCanSale());
		newSku.setPaymentType(skuInput.getPaymentType());
		newSku.setDeliveryDesc(skuInput.getDeliveryDesc());
		newSku.setSkuNameCn(skuInput.getSkuNameCn());
		newSku.setSkuTitleSub(skuInput.getSkuTitleSub());
		newSku.setSkuUnit(skuInput.getSkuUnit());
		
		//更新时间&创建时间同步 ，创建的时候
		newSku.setUpdateTime(timeService.getDate());
		newSku.setUpdateUserId(endMerchantUserBo.getId());
		newSku.setUpdateUserName(endMerchantUserBo.getLoginName());
		
		int rows = skuMapper.updateByPrimaryKeySelective(newSku);
		if(rows>0){
			return newSku.getId();
		}else{
			String message = "更新sku失败"+JsonUtils.beanToJson(sku);
			LOGGER.error(message);
			throw new RuntimeException(message); 
		}
	}
    
}
